﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
    using System.Web.Http.Filters;

namespace TinCan.LRS.Web.Filters
{
    public class AddOptionsSupportAttribute : ActionFilterAttribute
    {
        public string SupportedMethods { get; set; }

        /// <summary>
        /// Adds the header "Access-Control-Allow-Methods" with the supplied value
        /// </summary>
        /// <param name="supportedMethods">Comma delimited list of supported methods</param>
        public AddOptionsSupportAttribute(string supportedMethods = "GET, POST, PUT, DELETE, OPTIONS")
        {
            SupportedMethods = supportedMethods;
        }

        public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
        {
            if (actionExecutedContext.Response != null)
                actionExecutedContext.Response.Headers.Add("Access-Control-Allow-Methods", SupportedMethods);

            base.OnActionExecuted(actionExecutedContext);
        }
    }
}